<%-- 
    Document   : category
    Created on : Apr 28, 2013, 5:41:18 PM
    Author     : Lord Makuta
--%>

<%@page import="java.io.InputStreamReader"%>
<%@page import="java.io.BufferedReader"%>
<%@page import="java.io.FileInputStream"%>
<%@page import="java.io.FileInputStream"%>
<%@page import="java.io.File"%>
<%@page import="java.util.Vector"%>
<%@page import="db.UserDAO"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="db.MovieObj"%>
<%@page import="java.sql.Connection"%>
<%@page import="db.DB"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <%
        String genre=request.getParameter("genre");
        String title=genre;
        if (genre.equals("Best"))
        {
            title+=" Selling Movies";
        }
        else if (genre.equals("Reco"))
        {
            title+="mended for You";
        }
        else
        {
            title+=" Movies";
        }
        
        Vector<MovieObj> movies=new Vector<MovieObj>();
        DB data=new DB();
        Connection conn=null;
        try
        {
            conn=data.getConnection();
            Statement stm=conn.createStatement();
            ResultSet res=null;
            
            if (genre.equals("Best"))
            {
                res=stm.executeQuery("SELECT batch.Id, batch.Name, batch.Type FROM (SELECT COUNT(R.MovieId) AS cnt, M.Id, M.Name, M.Type, M.Rating "
                    + "FROM Movie M, Rental R WHERE M.Id=R.MovieId GROUP BY R.MovieId ORDER BY cnt DESC) as batch ORDER BY batch.Rating DESC, batch.cnt DESC");
            }
            else if (genre.equals("Reco"))
            {
                res=((UserDAO)session.getAttribute("user")).getRecoQ().executeQuery();
            }
            else
            {
                res=stm.executeQuery("SELECT Id, Name, Type FROM Movie WHERE Type='"+genre+"'");
            }
            
            int i=0;
            while (res.next())// && i<30)
            {
                movies.add(new MovieObj(res.getInt(1), res.getString(2), res.getString(3)));
                i++;
            }
            if (movies.size()==0)
                movies.add(null);
        }
        catch (Exception e)
        {
            
        }
        finally
        {
            try{conn.close();}catch(Exception ee){};
        }
    %>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>DataFlix - <%=title%></title>
        
        <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
	<meta http-equiv="content-language" content="en" />
	<meta name="description" content="" />
	<meta name="language" content="en" />
	
	<link type="text/css" rel="stylesheet" href="./styles/fonts.css"/>
	<link type="text/css" rel="stylesheet" href="./styles/header.css"/>
	<link type="text/css" rel="stylesheet" href="./styles/fancybox.css"/>
	<link type="text/css" rel="stylesheet" href="./styles/images.css"/>
	
	<link type="text/css" rel="stylesheet" href="./styles/charcoal.css"/>
	<link type="text/css" rel="stylesheet" href="./styles/category.css"/>
	
        
    </head>
    <body>
        
        <jsp:include page="./header.jsp" />
        <div id=<%="\"title"+genre+"\""%>><%=title%></div>
        
        <ul class="contentRow" id=<%="\""+genre+"\""%>>
            
        <%  
            int i=0;
            String path=null;   File f=null;    FileInputStream fis=null;   InputStreamReader inStream=null;    BufferedReader buff=null;
            for (int j=0; j<movies.size(); j++)
            {
                if (movies.get(j)!=null)
                {
        %>                       
        <li <%if (i-3>=0) out.write("class=\"expLef\"");%> ><a class="fancyBox" <%="href=./movie.jsp?id="+movies.get(j).getId()%> >
                <img <%="id=\"movie"+movies.get(j).getId()+"\""%> <%="style=\"background-image: url(./images/movies/"+movies.get(j).getGenre().toLowerCase()+"/movie"+movies.get(j).getId()+".jpg\""%>/>
                <div class="overlay">
                    <h1><%=movies.get(j).getTitle()%></h1>
                        <div class="shortDesc">
                            <%
                                    path=application.getRealPath("/");
                                    f=new File(path+"images/movies/"+movies.get(j).getGenre().toLowerCase()+"/movie"+movies.get(j).getId()+"Desc.txt");
                                    fis=new FileInputStream(f);
                                    inStream = new InputStreamReader(fis);
                                    buff = new BufferedReader(inStream);

                                    path=buff.readLine();
                                    out.print(path.substring(0, 205)+"...");
                                %>
                        </div>
                </div>
        </a></li>
        <%      i++;
                }
                else
                {
                    out.print("<h1 style=\"display:inline-block;\">You haven't rented any movies yet, rent some so we can find something you'll like</h1>");
                }                   
                if (i==6)
                    i=0;
            } %>
            
        </ul>
        
        <%--<div id="pageBtns">
            <a class=<%="\""+genre.toLowerCase()+"Btn"+"\""%> href="./">&#8617; Previous page</a>
            <a class=<%="\""+genre.toLowerCase()+"Btn"+"\""%> href="./">Next page &#10143;</a>
	</div>--%>
	
	<div id="footer"></div>
    </body>
</html>
